package com.dayu.finecomm.dsc;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.socket.DatagramPacket;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

import com.dayu.finecomm.dsc.net.DSCNetManager;

/**
 * 保存DTU设备相关信息及通信处理
 * 
 * @author hddz
 * @since 2017-07-28 13:17:00
 * @version V1.0
 */
public class DTUProxy {
	/**
	 * DTU注册状态
	 */
	public static final int DTU_UNREGISTED = 0; // 未注册
	public static final int DTU_REGISTED = 1; // 已注册

	/**
	 * DTU注册状态
	 */
	int mState;

	/**
	 * 该DTU代理的ID号
	 */
	int mID = 0;

	/**
	 * 通信通道
	 */
	Channel mChannel;

	/**
	 * 身份识别码
	 */
	String mIdentify = null;

	/**
	 * 通信方式 1 - TCP， 2 - UDP
	 */
	int mCommType;

	/**
	 * DTU发送IP地址
	 */
	String mRemoteIP;

	/**
	 * DTU发送端口
	 */
	int mRemotePort;

	/**
	 * 
	 * @param id
	 *            DTU代理ID号
	 * @param channel
	 *            通信通道
	 */
	public DTUProxy(int id, String socket, Channel channel, int type,
			String identify) {
		mState = DTU_UNREGISTED;
		mID = id;
		mChannel = channel;
		mCommType = type;
		mIdentify = identify;

		if (null != socket) {
			String socketString = socket;
			int colonAt = socketString.indexOf(":");

			mRemoteIP = socketString.substring(1, colonAt);
			mRemotePort = Integer.parseInt(socketString.substring(colonAt + 1));
		}
	}

	/**
	 * 关闭通信端口
	 */
	public void close() {
		if (null != mChannel) {
			mChannel.close();
		}
	}

	public void reset(String socket, Channel channel) {
		mState = DTU_UNREGISTED;

		mChannel = channel;

		if (null != socket) {
			String socketString = socket;
			int colonAt = socketString.indexOf(":");

			mRemoteIP = socketString.substring(1, colonAt);
			mRemotePort = Integer.parseInt(socketString.substring(colonAt + 1));
		}
	}

	/**
	 * 获取网络通信方式
	 * 
	 * @return 1 - TCP， 2 - UDP
	 */
	public int getCommType() {
		return mCommType;
	}

	/**
	 * 获取身份识别码
	 * 
	 * @return
	 */
	public String getIdentify() {
		return mIdentify;
	}

	/**
	 * DTU注册
	 * 
	 * @param ip
	 *            DTU本地移动IP
	 * @param port
	 *            DTU本地移动端口
	 */
	public void registed() {
		mState = DTU_REGISTED;
	}

	/**
	 * DTU注销
	 */
	public void unregisted() {
		mState = DTU_UNREGISTED;
	}

	/**
	 * DTU是否注册
	 * 
	 * @return true or false
	 */
	public boolean isRegisted() {
		return mState == DTU_REGISTED ? true : false;
	}

	/**
	 * DSC向DTU发送数据
	 * 
	 * @param msg
	 * @throws UnknownHostException
	 */
	public void sendData(byte[] msg) throws UnknownHostException {
		if (null != mChannel && null != msg) {
			if (DSCNetManager.DSC_NET_TCP == mCommType) {
				ByteBuf bufAck = Unpooled.buffer(msg.length);
				bufAck.writeBytes(msg);
				DSCNetManager.getInstance().Send(mChannel, bufAck);
			} else {
				ByteBuf bufAck = Unpooled.copiedBuffer(msg);
				InetAddress addr = InetAddress.getByName(mRemoteIP);
				InetSocketAddress remoteAddress = new InetSocketAddress(addr,
						mRemotePort);
				DatagramPacket rawData = new DatagramPacket(bufAck,
						remoteAddress);
				DSCNetManager.getInstance().Send(mChannel, rawData);
			}
		}
	}
}
